<% @title = t(".api_keys") %>

<% if @api_key %>
  <div class="t-body push--bottom-s">
    <p>
      <%= t(".save_key") %>
      <span> <code><%= @api_key %></code> </span>
    </p>
  </div>
<% end %>

<header class="gems__header">
  <p class="gems__meter">
    <%= page_entries_info @api_keys, entry_name: 'API keys' %>
  </p>
</header>

<div class="t-body">
  <table class="owners__table">
    <tr class="owners__row owners__header">
      <th class="owners__cell">
        <%= t(".name") %>
      </th>
      <th class="owners__cell">
        <%= t(".scopes") %>
      </th>
      <th class="owners__cell">
        <%= t(".gem") %>
      </th>
      <th class="owners__cell">
        <%= t(".age") %>
      </th>
      <% if current_user.mfa_enabled? %>
        <th class="owners__cell">
          <%= t(".mfa") %>
        </th>
      <% end %>
      <th class="owners__cell">
        <%= t(".last_access") %>
      </th>
      <th class="owners__cell">
        <%= t(".expiration") %>
      </th>
      <th class="owners__cell">
      </th>
      <th class="owners__cell">
      </th>
    </tr>

    <% for api_key in @api_keys do %>
      <tr class="owners__row <%= 'owners__row__invalid' if api_key.soft_deleted? %>">
        <td class="owners__cell" data-title="Name">
          <%= api_key.name %>
        </td>
        <td class="owners__cell" data-title="Scopes">
          <% api_key.scopes.each do |scope| %>
            <ul class="scopes__list"><%= t(".#{scope}") %></ul>
          <% end %>
        </td>
        <td class="owners__cell" data-title="Gem">
          <%= gem_scope(api_key) %>
        </td>
        <td class="owners__cell" data-title="Age">
          <%= time_ago_in_words(api_key.created_at) %>
        </td>
        <% if current_user.mfa_enabled? %>
          <td class="owners__cell" data-title="MFA">
            <%= image_tag("/images/check.svg") if api_key.mfa? %>
          </td>
        <% end %>
        <td class="owners__cell" data-title="Last access">
          <%= api_key.last_accessed_at&.strftime("%Y-%m-%d %H:%M %Z") %>
        </td>
        <td class="owners__cell" data-title="Expiration">
          <%= api_key.expires_at&.strftime("%Y-%m-%d %H:%M %Z") %>
        </td>
        <td class="owners__cell">
          <% unless api_key.soft_deleted? %>
            <%= button_to t("edit"),
                        edit_profile_api_key_path(id: api_key.id),
                        method: :get,
                        class: "form__submit form__submit--small" %>
          <% end %>
        </td>
        <td class="owners__cell">
          <%= button_to t(".delete"),
                      profile_api_key_path(id: api_key.id),
                      method: :delete,
                      class: "form__submit form__submit--small",
                      data: { confirm: t(".confirm")} %>
        </td>
      </tr>
    <% end %>

    <tr class="owners__row">
      <td class="owners__cell">
      </td>
      <td class="owners__cell">
      </td>
      <td class="owners__cell">
      </td>
      <td class="owners__cell">
      </td>
      <td class="owners__cell">
      </td>
      <td class="owners__cell">
      </td>
      <td class="owners__cell">
      </td>
      <% if current_user.mfa_enabled? %>
        <td class="owners__cell">
        </td>
      <% end %>
      <td class="owners__cell">
        <%= button_to t(".reset"),
                    reset_profile_api_keys_path,
                    method: :delete,
                    class: "form__submit form__submit--small",
                    data: { confirm: t(".confirm_all")} %>
      </td>
    </tr>
  </table>

  <%= paginate @api_keys %>

  <p><%= button_to t(".new_key"), new_profile_api_key_path, method: "get", class: "form__submit" %></p>
  <% if current_user.oidc_api_key_roles.any? %>
    <h2 class="t-display"><%= link_to t("oidc.api_key_roles.index.api_key_roles"), profile_oidc_api_key_roles_path, class: "t-link t-underline" %> →</h2>
  <% end %>
</div>
